package com.angel.web.health;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.angel.core.util.JsonUtil;
import com.angel.core.util.Pager;
import com.angel.health.BuzDoctor;
import com.angel.provider.health.BuzDoctorProvider;



/**
 * @author gao
 *
 */

@Controller
@RequestMapping("/doctor")
public class BuzDoctorController {

	@Autowired
	private BuzDoctorProvider provider;
	
	//分頁查詢
	@RequestMapping("/index")
	public String index(BuzDoctor doctor,HttpServletRequest request,Pager pager){
		Pager bean = provider.findAll(doctor,pager);
		request.setAttribute("pager", bean);
		request.setAttribute("doctor", doctor);
		return "doctor/doctor";
	}
	
	//跳转到添加页面
	@RequestMapping("/add")
	public String add(){
		return "doctor/doctor_add";
	}
	
	//保存
	@RequestMapping("/save")
	@ResponseBody
	public String save(BuzDoctor doctor,HttpServletRequest request){
		doctor.setCreateBy(request.getSession().getAttribute("user").toString());
		doctor.setDoctorFrom(0);
		Map<String, String>	map = provider.save(doctor);
		return JsonUtil.toJson(map);
	}
	
	//根據id查詢
	@RequestMapping("/look")
	public String edit(BuzDoctor doctor,HttpServletRequest request,String method){
		BuzDoctor buzDoctor = provider.findById(doctor.getId());
		if(buzDoctor != null && buzDoctor.getPhysicianQualificationCertificate() != null && buzDoctor.getPhysicianQualificationCertificate() != ""){
			buzDoctor.setPaths(buzDoctor.getPhysicianQualificationCertificate().split(","));
		}else{
			buzDoctor.setPaths(null);
		}
		request.setAttribute("doctor", buzDoctor);
		request.setAttribute("method", method);
		return "doctor/doctor_look";
	}

	//跳转到审核页面
	@RequestMapping("/checkDoctor")
	public String checkDoctor(Integer id,HttpServletRequest request){
		BuzDoctor doctor = provider.findById(id);
		if(doctor != null && doctor.getPhysicianQualificationCertificate() != null && doctor.getPhysicianQualificationCertificate() != ""){
			doctor.setPaths(doctor.getPhysicianQualificationCertificate().split(","));
		}
		request.setAttribute("doctor", doctor);
		return "doctor/doctor_check";
	}
	
	//审核
	@RequestMapping(value="update",produces = "application/json; charset=utf-8")
	public String update(BuzDoctor doctor,HttpServletRequest request){
		provider.update(doctor);
		return "redirect:index";
	}
	
	//修改
	@RequestMapping(value="updateDoc",produces = "application/json; charset=utf-8")
	public String updateDoc(BuzDoctor doctor,HttpServletRequest request){
		provider.updateDoc(doctor);
		return "redirect:index";
	}
	
	//根据id查询用户
	@RequestMapping("/findDoctorById")
	@ResponseBody
	public String findDoctorById(BuzDoctor doctor){
		BuzDoctor buzDoctor = provider.findById(doctor.getId());
		return JsonUtil.toJson(buzDoctor);
	}
	
	//批量删除
	@RequestMapping("delete")
	@ResponseBody
	public String delete(String ids){
		int i = provider.delete(ids);
		return JsonUtil.toJson(i);
	}
	
	//删除单个用户
	@RequestMapping("deleteById")
	@ResponseBody
	public String deleteById(BuzDoctor doctor){
		int i = provider.deleteById(doctor);
		return JsonUtil.toJson(i);
	}
}
